// components/selector-single/selector-single.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Array,
      observer (e) {
        const cloneE = JSON.parse(JSON.stringify(e))
        cloneE.forEach((item, index) => {
          if (typeof item === 'object') {
            cloneE[index] = item.tagName
          }
        })
        this.setData({
          _data: cloneE
        })
      }
    },
    _data: {
      type: Array
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    selected: '',
    id: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onOptionTap(e) {
      const selected = e.currentTarget.dataset.item
      const id = this.data.data.filter(i => i.tagName == selected)[0]
      this.setData({
        selected,
        id: id ? id.id : ''
      })
      this.triggerEvent('checked', id ? {id: id.id, selected} : selected,)
    },

    reset() {
      this.setData({
        selected: '',
        id: ''
      })
    }
  }
})
